package org.icress.fusion.b2b

class ContractController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    def contractService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
		params.max = Math.min(params.max ? params.int('max') : 20, 100)
        render view : '/b2b/contract/list', model : [contractInstanceList: contractService.list(params), contractInstanceTotal: contractService.count()]
    }

    def create = {
        def contractInstance = new Contract()
        contractInstance.properties = params
        render view : '/b2b/contract/create', model : [contractInstance: contractInstance]
    }

    def save = {
        def contractInstance = new Contract(params)
        
        if (contractService.save(contractInstance)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), contractInstance.id])}"
            redirect(action: "show", id: contractInstance.id)
        }else{
            render(view: "/b2b/contract/create", model: [contractInstance: contractInstance])
        }
    }

    def show = {
        def contractInstance = contractService.show(params.id)
        if (!contractInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/b2b/contract/show', model : [contractInstance: contractInstance]
        }
    }

    def edit = {
        def contractInstance = contractService.edit(params.id)
        if (!contractInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/b2b/contract/edit', model : [contractInstance: contractInstance]
        }
    }
    
    def update = {
        def contractInstance = contractService.edit(params.id)
        if (contractInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (contractInstance.version > version) {
                    contractInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'b2b.contract.label', default: 'Contract')] as Object[], "Another user has updated this Contract while you were editing")
                    render(view: "/b2b/contract/edit", model: [contractInstance: contractInstance])
                    return
                }
            }
            contractInstance.properties = params
            
            if (contractService.update(contractInstance)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), contractInstance.id])}"
                redirect(action: "show", id: contractInstance.id)
            }else {
                render(view: "/b2b/contract/edit", model: [contractInstance: contractInstance])
            }
        } else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def contractInstance = contractService.delete(params.id)
        if (contractInstance) {
            try {
                contractInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'b2b.contract.label', default: 'Contract'), params.id])}"
            redirect(action: "list")
        }
    }
}
